<?php
	/**
	 * @package AmpleHarvest
	 */
	use \ampleharvest\code;
	require_once(dirname(dirname(dirname(__FILE__)))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();
	
	require_once dirname(dirname(__FILE__)) . '/cStomp.php';

	require_once $codeConfig->C_STR_PHP_MAILER_PATH . '/class.phpmailer.php';


	/**
	 * @brief  Class to handle sending Email notifications
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 24 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1  
	 * @ingroup code classes
	 */
	class cMailNotification
	{
		/**
		 * From Address
		 * - Type: string
		 */
		protected $m_str_From = null;

		/**
		 * Holds any error information returned while sending mail.
		 * - Type: string
		 */
		protected $m_str_Error = "";

		/**
		 * to Address
		 * - Type: array
		 */
		protected $m_arr_To = array();

		/**
		 * CC Address
		 * - Type: array
		 */
		protected $m_arr_CC = array();

		/**
		 * Subject
		 * - Type: string
		 */
		protected $m_str_Subject = "";

		/**
		 * Body
		 * - Type: string
		 */
		protected $m_str_Body = "";

		public function __sleep()
		{
			return array('m_arr_cc', 'm_arr_To', 'm_str_Body', 'm_str_Error', 'm_str_From', 'm_str_Subject');
		}

		/**
		 * This function will add a TO address
		 * @param string $str_Email this is the email address to add
		 */
		public function add_To($str_Email)
		{
			$this->m_arr_To[] = $str_Email;
		}

		/**
		 * This function will add a TO address
		 * @param string $str_Email this is the email address CC add
		 */
		public function add_CC($str_Email)
		{
			$this->m_arr_CC[] = $str_Email;
		}

		/**
		 * Sends the Email to STOMP QUEue
		 * @return boolean true if it was sent false other wise.
		 */
		public function send()
		{
			$obj_Stomp = new \cStomp();
			if ((sizeof($this->m_arr_To) < 1) && (sizeof($this->m_arr_CC) < 1))
			{
				$this->set_Error("This email has no addressees.");
				return false;
			}
			return $obj_Stomp->send(\cStomp::C_STR_QUEUE_EMAIL, $this->get_AsJson());
		}

		public function get_AsJson()
		{
			$obj_Email = new stdClass();

			$obj_Email->cc = $this->m_arr_CC;
			$obj_Email->to = $this->m_arr_To;
			$obj_Email->body = $this->get_Body();
			$obj_Email->from = $this->get_From();
			$obj_Email->subject = $this->get_Subject();

			return json_encode($obj_Email);
		}

		public function load_FromJson($str_Json)
		{
			$obj_Email = json_decode($str_Json);

			$this->m_arr_CC = $obj_Email->cc;
			$this->m_arr_To = $obj_Email->to;
			$this->set_Body(nl2br($obj_Email->body));
			$this->set_From($obj_Email->from);
			$this->set_Subject($obj_Email->subject);

			return json_encode($obj_Email);
		}

		public function send_JSONMail($str_Json)
		{
			$this->load_FromJson($str_Json);
			return $this->send_EMail();
		}

		/**
		 * Sends the Email
		 * @return boolean true if it was sent false other wise.
		 */
		public function send_EMail()
		{
			ob_start();
			$mail = new PHPMailer();
			
			$mail->IsSMTP();
			
			
			 //GMAIL config
			 $mail->SMTPAuth = true; // enable SMTP authentication
			 $mail->SMTPSecure = "ssl"; // sets the prefix to the server
			 $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
			 $mail->Port = 465; // set the SMTP port for the GMAIL server
			 $mail->Username = ""; // GMAIL username
			 $mail->Password = ""; // GMAIL password
			 //End Gmail
			 /*
			//Local Sendmale config
			$mail->SMTPAuth = true; // enable SMTP authentication
			$mail->SMTPSecure = "ssl"; // sets the prefix to the server
			$mail->Mailer = "smtp";
			$mail->SMTPDebug = false;
			$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
			$mail->Port = 465; // set the SMTP port for the GMAIL server
			$mail->Username = ""; // GMAIL username
			$mail->Password = ""; // GMAIL password
			//End Local Sendmale config
			*/
			$mail->From = "NoReply@AmpleHarvest.com";
			$mail->FromName = "AmpleHarvest";
			$mail->Subject = $this->get_Subject();
			//print($this->m_str_Body);
			$mail->MsgHTML($this->get_Body());
			
			//$mail->AddReplyTo("reply@email.com","reply name");//they answer here, optional
			foreach ($this->m_arr_To as $To)
			{
				if ($To)
				{
					$mail->AddAddress($To);
				}
			}

			foreach ($this->m_arr_CC as $CC)
			{
				if ($CC)
				{
					$mail->AddCC($CC);
				}
			}
			//$mail->AddAddress("address@to.com", "name to");
			$mail->IsHTML(true); // send as HTML
			
			//$this->debug_Output2HTML();
			$bool_Success = $mail->Send();

			$str_Return = ob_get_clean();

			//print($str_Return);

			if (!$bool_Success)
			{ //to see if we return a message or a value bolean
				//echo "Mailer Error: " . $mail->ErrorInfo;
				$this->m_str_Error = $str_Return;
				return false;
			}
			return true;
		}

		/**
		 *@{
		 *@name Setters
		 */

		/**
		 * Sets the text of the body
		 * @param string $str_Body Text that you would like placed in the body of the email
		 */
		public function set_Body($str_Body)
		{
			$this->m_str_Body = $str_Body;
		}

		public function set_Error($str_Msg)
		{
			$this->m_str_Error = $str_Msg;
		}

		public function set_From($str_From)
		{
			$this->m_str_From = $str_From;
		}

		/**
		 * Sets the Subject of the email
		 * @param string $str_Subject This is the subject of the email
		 */
		public function set_Subject($str_Subject)
		{
			$this->m_str_Subject = $str_Subject;
		}

		/**
		 *@}
		 */
		/**
		 *@{
		 *@name Getters
		 */
		/**
		 * Returns error messages collected while sending email.
		 * @return string empty if no message
		 */
		public function get_Error()
		{
			return $this->m_str_Error;
		}

		/**
		 * Gets the content of the email
		 * @return string Text placed in the email
		 */
		public function get_Body()
		{
			return $this->m_str_Body;
		}

		/**
		 * Returns the Carbon Copy Adresses as a string
		 * @return string Returns the CC Addresses as a "; " sepearated String
		 */
		public function get_CC()
		{
			return implode("; ", $this->m_arr_CC);
		}

		/**
		 * Gets the from Address
		 * @return string The address the email will show as from
		 */
		public function get_From()
		{
			return $this->m_str_From;
		}

		/**
		 * Returns the subject of the email
		 * @return string Subject Line of the email
		 */
		public function get_Subject()
		{
			return $this->m_str_Subject;
		}

		/**
		 * Returns the to Adresses as a string
		 * @return string Returns the to Addresses as a "; " sepearated String
		 */
		public function get_To()
		{
			return implode("; ", $this->m_arr_To);
		}

		/**
		 *@}
		 */

		/**
		 * Debuging out put this will print all the data of the object to the screen.
		 */
		public function debug_Output2HTML()
		{
			print("From : " . $this->get_From() . "<br/>");
			print("To : " . $this->get_To() . "<br/>");
			print("CC : " . $this->get_CC() . "<br/>");
			print("Subject : " . $this->get_Subject() . "<br/>");
			print("Body : " . $this->get_Body() . "<br/>");
		}
	}
?>